/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package inet.dao;

import inet.entity.Branch;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author MiSoKo
 */
public class BranchDAO extends BaseDAO {
    //    lấy danh sách tất cả nhân viên còn đnag 

    public List<Branch> getAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = null;
        List<Branch> lstBranch = null;
        try {
            sql = "SELECT * FROM BRANCH";
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            Branch branch = null;
            while (rs.next()) {
                branch = new Branch();
                if (lstBranch == null) {
                    lstBranch = new ArrayList<Branch>();
                }
                int i = 1;
                branch.setBranch_id(rs.getBigDecimal(i++));
                branch.setBranch_name(rs.getString(i++));
                branch.setDescription(rs.getString(i++));
                branch.setLocation(rs.getString(i++));
                branch.setCom_id(rs.getBigDecimal(i++));
                lstBranch.add(branch);
            }
            return lstBranch;
        } catch (Exception ex) {
            System.out.println("Error in sql: " + sql);
            ex.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }

    //Hien thi theo ID
    public Branch getById(BigDecimal id) {
        Branch branch = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM BRANCH "
                + "WHERE BRANCH_ID = ?";

        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.setBigDecimal(1, id);
            rs = ps.executeQuery();
            int i = 1;
            while (rs.next()) {
                branch = new Branch();
                branch.setBranch_id(rs.getBigDecimal(i++));
                branch.setBranch_name(rs.getString(i++));
                branch.setDescription(rs.getString(i++));
                branch.setLocation(rs.getString(i++));
                branch.setCom_id(rs.getBigDecimal(i++));
//                System.out.print(rs.getString(2));
//                System.out.print(rs.getString(3));
//                System.out.print(rs.getString(4));
            }
            return branch;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            releaseConnection(conn, ps, rs);
        }
        return null;
    }

}
